/**
 * 
 */
package com.demo2do.boyle.client.handler.impl;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.util.WebUtils;

import com.demo2do.boyle.client.handler.CookieHandler;

/**
 * @author lute, Wilson
 *
 */
@Component("cookieHandler")
public class CookieHandlerImpl implements CookieHandler {

    private static final Logger logger = LoggerFactory.getLogger(CookieHandlerImpl.class);

    @Value("#{config['domain']}")
    private String domain;

    @Value("#{config['cookie.expire-time']}")
    private int expireTime;

    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.handler.CookieHandler#getCookie(java.lang.String, javax.servlet.http.HttpServletRequest)
     */
    public String getCookie(String name, HttpServletRequest request) {
        Cookie cookie = WebUtils.getCookie(request, name);
        return cookie == null ? null : cookie.getValue();
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.handler.CookieHandler#addCookie(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public void addCookie(String name, String value, HttpServletRequest request, HttpServletResponse response) {

        logger.info("CookieHandlerImpl#addCookie - start adding cookie for [" + name + "], value: " + value + ".");

        Cookie cookie = WebUtils.getCookie(request, name);

        if (cookie == null) {
            logger.info("CookieHandlerImpl#addCookie - new cookie for [" + name + "], value: " + value + ".");
            cookie = new Cookie(name, value);
        }

        cookie.setPath("/");
        cookie.setDomain(this.domain);
        cookie.setMaxAge(this.expireTime);

        response.addCookie(cookie);
        logger.info("CookieHandlerImpl#addCookie - successfully added cookie for [" + name + "], value: " + value + ".");

    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.handler.CookieHandler#addCookie(java.lang.String, java.lang.String, int, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public void addCookie(String name, String value, int expireTime, HttpServletRequest request, HttpServletResponse response) {

        if (logger.isTraceEnabled()) {
            logger.trace("CookieHandlerImpl#addCookie - add cookie name[" + name + "], value[" + value + "] and expire time[" + String.valueOf(expireTime) + "].");
        }

        Cookie cookie = WebUtils.getCookie(request, name);

        if (cookie == null) {
            cookie = new Cookie(name, value);
        }

        cookie.setPath("/");
        cookie.setDomain(this.domain);
        cookie.setMaxAge(expireTime);

        response.addCookie(cookie);

    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.handler.CookieHandler#deleteCookie(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public void deleteCookie(String name, HttpServletRequest request, HttpServletResponse response) {

        if (logger.isTraceEnabled()) {
            logger.trace("CookieHandlerImpl#deleteCookie - delete cookie name[" + name + "]");
        }

        Cookie cookie = WebUtils.getCookie(request, name);

        if (cookie != null) {

            cookie.setValue("");
            cookie.setPath("/");
            cookie.setDomain(this.domain);
            cookie.setMaxAge(0);

            response.addCookie(cookie);
        }

    }

}
